package com.cxyd.modules.wcs.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cxyd.common.constant.Constants;
import com.cxyd.common.service.impl.CrudServiceImpl;
import com.cxyd.common.utils.ConvertUtils;
import com.cxyd.common.utils.JsonUtils;
import com.cxyd.modules.api.dto.AgvStateResponseDTO;
import com.cxyd.modules.api.dto.RcsResultHK;
import com.cxyd.modules.wcs.dao.AgvStateDao;
import com.cxyd.modules.wcs.dto.AgvStateDTO;
import com.cxyd.modules.wcs.entity.AgvStateEntity;
import com.cxyd.modules.wcs.service.AgvStateService;
import com.fasterxml.jackson.core.type.TypeReference;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class AgvStateServiceImpl extends CrudServiceImpl<AgvStateDao, AgvStateEntity, AgvStateDTO> implements AgvStateService {
    @Override
    public QueryWrapper<AgvStateEntity> getWrapper(Map<String, Object> params) {
        return null;
    }

    @Override
    public void agvStateHandle(AgvStateResponseDTO response) {
        List<AgvStateDTO> stateList = response.getData();
//        循环处理每个状态数据
        for (AgvStateDTO state : stateList) {
            String robotCode = state.getRobotCode();
//            根据小车编号查询数据库中是否存在该小车状态数据，如果存在则更新，不存在则新增
            AgvStateEntity agvStateEntity = baseDao.selectOne(new QueryWrapper<AgvStateEntity>().eq("robot_code", robotCode));
            if (agvStateEntity == null) {
                AgvStateEntity entity = ConvertUtils.sourceToTarget(state, AgvStateEntity.class);
                baseDao.insert(entity);
            } else {
                agvStateEntity.setBattery(state.getBattery());
                agvStateEntity.setPosX(state.getPosX());
                agvStateEntity.setPosY(state.getPosY());
                agvStateEntity.setSpeed(state.getSpeed());
                agvStateEntity.setStatus(state.getStatus());
                agvStateEntity.setStop(state.getStop());
                agvStateEntity.setPodCode(state.getPodCode());
                agvStateEntity.setPodDir(state.getPodDir());
                agvStateEntity.setUpdateDate(new Date());
                baseDao.updateById(agvStateEntity);
            }
        }
    }
}
